//vector 内存交换
#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int> &v)
{
    for(auto iter=v.begin();iter!=v.end();iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
}

int main()
{
    vector<int> v1={1,2,3,4,5};
    vector<int> v2={9,8,7,6,5};
    cout<<"v1:";
    printVector(v1);

    cout<<"v2:";
    printVector(v2);

    //交换 v1 和 v2 的内容
    v1.swap(v2);
    cout<<"v1:";
    printVector(v1);

    cout<<"v2:";
    printVector(v2);

    //缩容
    v1.resize(10000000);
    v1.resize(5);
    cout<<"v1.capacity():"<<v1.capacity()<<endl;

    //通过交换空的vector来缩减容量
    vector<int> (v1).swap(v1);//创建一个临时的vector并交换，以减少v1的容量
    cout<<"v1.capacity():"<<v1.capacity()<<endl;

    //内存清理
    v2.resize(10000);
    v2.clear();
    cout<<"v2.capacity():"<<v2.capacity()<<endl;

    //通过交换空的vector来清理内存
    vector<int> ({}).swap(v2);
    cout<<"v2.capacity():"<<v2.capacity()<<endl;

    return 0;
}